package com.zhu.esyblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.esyblog.Utils.BeanCopyUtils;
import com.zhu.esyblog.Utils.UserThreadLocal;
import com.zhu.esyblog.pojo.Comment;
import com.zhu.esyblog.mapper.CommentMapper;
import com.zhu.esyblog.pojo.RespBean;
import com.zhu.esyblog.pojo.SysUser;
import com.zhu.esyblog.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.esyblog.service.ISysUserService;
import com.zhu.esyblog.vo.CommentParam;
import com.zhu.esyblog.vo.CommentVo;
import com.zhu.esyblog.vo.UserVo;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stx
 * @since 2022-06-22
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ISysUserService userService;
    /**
     * 根据文章id查询评论
     * @param id
     * @return
     */
    @Override
    public RespBean commentsByArticleId(Long id) {
        /**
         * 1.根据文章id查询评论列表
         * 2.根据作者的id查询作者的id
         * 3.判断 如果level=1 查询他的子评论
         * 4.有据评论id进行查询（parent_id）
         *
         */
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,id);
        queryWrapper.eq(Comment::getLevel,1);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVos = copyList(comments);
        return RespBean.success("success",commentVos);
    }

    /**
     * 评论文章
     * @param commentParam
     * @return
     */
    @Override
    public RespBean comment(CommentParam commentParam) {
        SysUser user = UserThreadLocal.get();
        Comment comment=new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(user.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parentId = commentParam.getParentId();
        if(parentId==null||parentId==0){
            comment.setLevel(1);
        }else {
            comment.setLevel(2);
        }
        comment.setParentId(parentId==null?0:parentId);
        Long toUid = commentParam.getToUid();
        comment.setToUid(toUid==null?0:toUid);
        commentMapper.insert(comment);
        return RespBean.success("success",null);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList=new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo=new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        //作者信息
        Long authorId = comment.getAuthorId();
        UserVo userVo = userService.findUserVoById(authorId);
        commentVo.setAuthor(userVo);
        //子评论
        Integer level = comment.getLevel();
        if(1==level){
            Long id = comment.getId();
            List<CommentVo> commentVoList=findCommontsByParentId(id);
            commentVo.setChildrens(commentVoList);
        }
        //to user  给谁评论
        if(level>1){
            Long toUid = comment.getToUid();
            UserVo userVoById = userService.findUserVoById(toUid);
            commentVo.setToUser(userVoById);
        }
  return  commentVo;
    }

    private List<CommentVo> findCommontsByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        return copyList(commentMapper.selectList(queryWrapper));
    }
}
